package com.example.service.impl;

import com.example.entity.MenuOrder;
import com.example.entity.PageBean;
import com.example.mapper.MenuOrderMapper;
import com.example.service.MenuOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MenuOrderServiceImpl implements MenuOrderService {
    @Autowired
    private MenuOrderMapper menuOrderMapper;

    @Override
    public void updata(MenuOrder menuOrder) {
        menuOrderMapper.updata(menuOrder);
    }

    @Override
    public void add(MenuOrder menuOrder) throws ParseException {
        if(menuOrder.getMenuorderTimestart()  == null){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date currentTime = dateFormat.parse(dateFormat.format(new Date()));
            menuOrder.setMenuorderTimestart(currentTime);
        }
        menuOrderMapper.insert(menuOrder);
    }

    @Override
    public void delete(String order_id,String menu_id) {
        menuOrderMapper.delete(order_id,menu_id);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, Integer orderid) {
        PageHelper.startPage(page,pageSize);
        List<MenuOrder> pageList=menuOrderMapper.selectByorderid(orderid);
        log.info("分页查询...");
        Page<MenuOrder> p=(Page<MenuOrder>) pageList;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public List<MenuOrder> MenuOrderByorderid(Integer orderid) {
        List<MenuOrder> pageList=menuOrderMapper.selectByorderid(orderid);
        return pageList;
    }

    @Override
    public void save(MenuOrder menuOrder) throws ParseException {
        MenuOrder menuOrder1 = MenuOrderByPOrderIdAndMenuId(menuOrder.getMenuId(),menuOrder.getOrderId());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentTime = dateFormat.parse(dateFormat.format(new Date()));
        menuOrder.setMenuorderTimestart(currentTime);
        if(menuOrder1 != null){
            updata(menuOrder);
        }
        else
            add(menuOrder);
    }

    @Override
    public MenuOrder MenuOrderByPOrderIdAndMenuId(Integer menuid, Integer orderid) {
        MenuOrder menuOrder=menuOrderMapper.selectByorderidandmenuid(menuid,orderid);
        return menuOrder;
    }
}
